import 'package:flutter/material.dart';

class AddressCreate extends StatefulWidget {
  const AddressCreate({super.key});

  @override
  State<AddressCreate> createState() => _AddressCreateState();
}

class _AddressCreateState extends State<AddressCreate> {
  bool _isAgreed = false;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Colors.white,
        elevation: 0,
        leading: IconButton(
          icon: const Icon(Icons.arrow_back_ios, color: Colors.black38),
          onPressed: () => Navigator.of(context).pop(),
        ),
        title: const Text(
          '新增收货地址',
          style: TextStyle(color: Colors.black, fontSize: 18),
        ),
        centerTitle: true,
        actions: const [SizedBox(width: 48)],
      ),
      body: Container(
        color: Colors.white,
        child: Column(
          spacing: 20,
          children: [
            InputMessage(title: '收货人：', placeholder: '请填写真实姓名，避免无法配送'),
            InputMessage(title: '手机号码：', placeholder: '收货人手机号码'),
            InputMessage(title: '所在地区：', placeholder: '请填写真实姓名，避免无法配送'),
            InputMessage(title: '详细地址：', placeholder: '街道、小区、楼栋门牌号等'),
            DefaultRadio(
              checked: _isAgreed,
              onChanged: (value) {
                setState(() {
                  _isAgreed = value;
                });
              },
            ),
            Container(
              padding: EdgeInsets.only(left: 30, right: 30),
              width: MediaQuery.of(context).size.width,
              child: ElevatedButton(
                style: ElevatedButton.styleFrom(backgroundColor: Colors.red),
                onPressed: () {},
                child: Text('保存并使用', style: TextStyle(color: Colors.white)),
              ),
            ),
          ],
        ),
      ),
    );
  }
}

class InputMessage extends StatelessWidget {
  final String title;
  final String placeholder;

  const InputMessage({Key? key, required this.title, required this.placeholder})
    : super(key: key);

  @override
  Widget build(BuildContext context) {
    return TextField(
      decoration: InputDecoration(
        prefixIcon: Container(
          padding: EdgeInsets.only(left: 16),
          width: 120,
          alignment: Alignment.centerLeft,
          child: Text(title, style: TextStyle(fontSize: 18)),
        ),
        prefixIconConstraints: BoxConstraints(minWidth: 0, minHeight: 0),
        hintText: placeholder,
        border: OutlineInputBorder(
          borderRadius: BorderRadius.circular(50),
          borderSide: BorderSide.none,
        ),
        contentPadding: EdgeInsets.symmetric(vertical: 12),
      ),
    );
  }
}

class DefaultRadio extends StatefulWidget {
  final bool checked;
  final ValueChanged<bool> onChanged;

  const DefaultRadio({
    super.key,
    required this.checked,
    required this.onChanged,
  });

  @override
  State<DefaultRadio> createState() => _DefaultRadioState();
}

class _DefaultRadioState extends State<DefaultRadio> {
  @override
  Widget build(BuildContext context) {
    return Container(
      padding: const EdgeInsets.symmetric(horizontal: 16),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.start,
        children: [
          // 自定义单选按钮（位置在左侧）
          Radio<bool>(
            value: true,
            groupValue: widget.checked,
            onChanged: (bool? value) {
              widget.onChanged(value ?? false);
            },
            activeColor: Colors.red, // 选中时为红色
          ),
          // 文字在右侧，设置为红色
          Text(
            '设置为默认地址',
            style: TextStyle(
              color: Colors.red, // 文字颜色为红色
              fontSize: 16,
            ),
          ),
        ],
      ),
    );
  }
}
